package dao.impl;

import dao.AjaxDao;
import model.Student;
import util.DBUtils;

import java.sql.ResultSet;
import java.util.Date;

public class AjaxDaoImpl extends DBUtils implements AjaxDao {
    @Override
    public Student getStudentByStuNo(String stuNo) {
        String sql = "select * from student where stuno = ?";
        Student student = null;
        try {
            resultSet = query(sql, stuNo);
            while (resultSet.next()) {
                student = new Student();
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuName(resultSet.getString("stuname"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setStuSex(resultSet.getInt("stusex"));
                student.setStuPhone(resultSet.getString("stuphone"));
                student.setStuEmail(resultSet.getString("stuemail"));
                student.setStuRegistered(resultSet.getString("sturegistered"));
                student.setStuAddress(resultSet.getString("stuaddress"));
                student.setStuProfession(resultSet.getString("stuprofession"));
                student.setStuIdnumber(resultSet.getString("stuidnumber"));
                student.setStuPolitice(resultSet.getString("stupolitice"));
                student.setStuRegdate(new Date(resultSet.getDate("sturegdate").getTime()));
                student.setStuStats(resultSet.getInt("stustats"));
                student.setStuIntroduction(resultSet.getString("stuintroduction"));
                student.setGid(resultSet.getInt("gid"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return student;
    }

    @Override
    public Student getStudentByStuName(String stuName) {
        String sql = "select * from student where stuname = ?";
        Student student = null;
        try {
            resultSet = query(sql, stuName);
            while (resultSet.next()) {
                student = new Student();
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuName(resultSet.getString("stuname"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setStuSex(Integer.parseInt(resultSet.getString("stusex")));
                student.setStuPhone(resultSet.getString("stuphone"));
                student.setStuEmail(resultSet.getString("stuemail"));
                student.setStuRegistered(resultSet.getString("sturegistered"));
                student.setStuAddress(resultSet.getString("stuaddress"));
                student.setStuProfession(resultSet.getString("stuprofession"));
                student.setStuIdnumber(resultSet.getString("stuidnumber"));
                student.setStuPolitice(resultSet.getString("stupolitice"));
                student.setStuRegdate(new Date(resultSet.getDate("sturegdate").getTime()));
                student.setStuStats(resultSet.getInt("stustats"));
                student.setStuIntroduction(resultSet.getString("stuintroduction"));
                student.setGid(resultSet.getInt("gid"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return student;
    }

    @Override
    public Student getStudentByStuIdNumber(String stuIdNumber) {
        String sql = "select * from student where stuidnumber = ?";
        Student student = null;
        try {
            resultSet = query(sql, stuIdNumber);
            while (resultSet.next()) {
                student = new Student();
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuName(resultSet.getString("stuname"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setStuSex(Integer.parseInt(resultSet.getString("stusex")));
                student.setStuPhone(resultSet.getString("stuphone"));
                student.setStuEmail(resultSet.getString("stuemail"));
                student.setStuRegistered(resultSet.getString("sturegistered"));
                student.setStuAddress(resultSet.getString("stuaddress"));
                student.setStuProfession(resultSet.getString("stuprofession"));
                student.setStuIdnumber(resultSet.getString("stuidnumber"));
                student.setStuPolitice(resultSet.getString("stupolitice"));
                student.setStuRegdate(new Date(resultSet.getDate("sturegdate").getTime()));
                student.setStuStats(resultSet.getInt("stustats"));
                student.setStuIntroduction(resultSet.getString("stuintroduction"));
                student.setGid(resultSet.getInt("gid"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return student;
    }
}
